//获取商品列表的父容器
  var productContainer = document.querySelector(".item-single");

productContainer.onclick = function (event) {
    var ele = event.target;
    console.log(ele);//打印输出所有次eve所有的
    console.log(ele.value);
    console.log(ele.getAttribute("type"));


    //=============== 加号=================
    if (ele.nodeName == 'INPUT'&& ele.value=="+"){
        //获得+ 前面哥哥的 属性（数量）
        var number = ele.previousElementSibling.value;
        // 在复制给数量
        ele.previousElementSibling.value = ++number;


        //获得单价  自己的根节点的 哥哥元素的最后一个孩子
        var unitPric = ele.parentElement.previousElementSibling.lastElementChild.innerHTML;
        //4. 获得小计 = 单价 * 数量
        ele.parentElement.nextElementSibling.lastElementChild.innerHTML = unitPric * number;

    }else if (ele.nodeName=='INPUT'&& ele.value=="-") {
        //================减号=============
        //获得数量
        var number = ele.nextElementSibling.value;
        if (number >= 1){
            //==============减少一个数量===================
            ele.nextElementSibling.value = --number;
        }else {
            number = 0;
        }
        //================获得单价===============
        var unitPric=ele.parentElement.previousElementSibling.lastElementChild.innerHTML;

        //4. 获得小计 = 单价 * 数量
        ele.parentElement.nextElementSibling.lastElementChild.innerHTML = unitPric * number;

    }

    sumPrice();

}

function sumPrice() {
    var sum =0;
    var ckboxes = document.querySelectorAll(".item-single input[type=checkbox]");
    for (var i = 0; i<ckboxes.length;i++){
        //    遍历整个数组，查询所有的ckbox的状态
        if (ckboxes[i].checked == true){
            var price = ckboxes[i].parentElement.parentElement.children[4].lastElementChild.innerHTML;
            console.log("商品的小计"+price);
            sum +=  parseFloat(price);
            console.log(sum);
        }
    }
    var jiesuanEle = document.querySelector(".jiesuan");
    jiesuanEle.children[3].innerText=sum;
}

//全选按钮
var selectAllEle = document.querySelector(".check-box-item input[type=checkbox]");
selectAllEle.onclick = function () {
    console.log(selectAllEle.checked);
    var ckboxer = document.querySelectorAll(".item-single input[type=checkbox]");
    for (var i = 0; i < ckboxer.length;i++){
        //让所有的check的状态跟全选的撞他相同
        ckboxer[i].checked = selectAllEle.checked;
    }
    sumPrice();
}

//去结算
function  jiesuan() {
    var cartIdList = "";
    var ckbox = document.querySelectorAll(".product-rows input[type='checkbox']");
    for (var i  = 0;i < ckbox.length; i++){
        if (ckbox[i].checked){
            var cartId = ckbox[i].value
            cartIdList += cartId + "-";
        }
    }
    document.querySelector(".submit-btn").attr("href","${pageContext.request.contextPath}/order/goPay/"+cartIdList)
}
